package com.zt.dao.code.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.support.rowset.SqlRowSetMetaData;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.zt.dao.base.impl.BaseJdbcSupport;
import com.zt.dao.code.IGenJdbcCodeDao;

/**
 * DAO类 GenJdbcCodeImpl
 * 
 * @author zhangzhw
 * @describe Jdbc代码实现
 */
public class GenJdbcCodeDaoImpl extends BaseJdbcSupport implements
		IGenJdbcCodeDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.zt.dao.code.impl.IGenJdbcCode#getMetaData(java.lang.String)
	 */
	public SqlRowSetMetaData getMetaData(String sql) {
		return super.queryForRowSet(sql).getMetaData();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.zt.dao.code.IGenJdbcCodeDao#testDataSource(java.lang.String,
	 * java.lang.String, java.lang.String)
	 */
	public DataSource testDataSource(String jdbcUrl, String user,
			String password) throws Exception {

		String oldJdbcUrl = "";
		String oldUser = "";
		String oldPassword = "";
		DataSource ds = super.getDataSource();
		ComboPooledDataSource dst = ((ComboPooledDataSource) ds);

		try {

			oldJdbcUrl = dst.getJdbcUrl();
			oldUser = dst.getUser();
			oldPassword = dst.getPassword();

			dst.setJdbcUrl("jdbc:oracle:thin:@" + jdbcUrl.trim());
			dst.setUser(user);
			dst.setPassword(password);

			super.getJdbcTemplate().setDataSource(dst);

			super.getJdbcTemplate().queryForList(
					"select sysdate from dual");

			return dst;
		} catch (Exception e) {
			dst.setJdbcUrl(oldJdbcUrl);
			dst.setUser(oldUser);
			dst.setPassword(oldPassword);
			super.getJdbcTemplate().setDataSource(dst);
			throw e;
		}

	}
}
